package com.zhuoli.zblog.repository.service.impl;

import com.zhuoli.zblog.repository.dao.UserMapper;
import com.zhuoli.zblog.repository.model.User;
import com.zhuoli.zblog.repository.model.UserExample;
import com.zhuoli.zblog.repository.service.UserDaoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Author: zhuoli
 * Description:
 * Date: 上午9:44 2018/4/14
 */
@Repository
public class UserDaoRepositoryImpl implements UserDaoRepository {

    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> queryUserByUserName(String userName) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUserNameEqualTo(userName);
        return userMapper.selectByExample(userExample);
    }

    @Override
    public User queryUserByUserPhone(String userPhone) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUserPhoneEqualTo(userPhone);
        List<User> userList = userMapper.selectByExample(userExample);
        return CollectionUtils.isEmpty(userList) ? null : userList.get(0);
    }

    @Override
    public void updateUserSelective(User user) {
        Long updateAt = System.currentTimeMillis() / 1000;
        user.setUpdateAt(Integer.parseInt(updateAt.toString()));

        UserExample userExample = new UserExample();
        userExample.createCriteria().andUserPhoneEqualTo(user.getUserPhone());

        userMapper.updateByExampleSelective(user, userExample);
    }

    @Override
    public void insertUser(User user) {
        Long updateAt = System.currentTimeMillis() / 1000;
        user.setUpdateAt(Integer.parseInt(updateAt.toString()));

        userMapper.insertSelective(user);
    }
}
